package com.mall.web.control.oss;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mall.web.util.OssUtil;

/**
 * This sample demonstrates how to get started with basic requests to Aliyun OSS 
 * using the OSS SDK for Java.
 */
@Controller
@RequestMapping("/eshop/oss/")
public class OssController {
    private String bucketFileNameEshop="eshop";
    @RequestMapping("osstest")
	public String ossIndex(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	
		return "eshop/back/osstest";
	}
    
    @RequestMapping("ossAction")
	public String ossAction(@RequestParam("uploadFile") MultipartFile[] uploadFile,HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	String key = "";
    	List<String> keyList = new ArrayList<String>();
    	try {
    		if(uploadFile != null && uploadFile.length > 0){
    			for(int i = 0;i < uploadFile.length-1;i++){
    				key = OssUtil.uploadImage(uploadFile[i], bucketFileNameEshop, request);
    				keyList.add(key);
        			OssUtil.uploadFileToOSS(key,uploadFile[i].getInputStream());
    			}
    			
    		}
		} catch (IOException e) {
			e.printStackTrace();
		}
    	modelMap.put("keyList", keyList);
    	return "eshop/back/ossshow";
	}
}
